//
//  XFForgetPassWordViewController.m
//  sharedBooks
//
//  Created by 赖国锋 on 2016/11/11.
//  Copyright © 2016年 XunFengKJ. All rights reserved.
//

#import "XFForgetPassWordViewController.h"
#import "UITextField+SMKLimit.h"
#import "YJGCDTimer.h"
#import "LoginViewController.h"

@interface XFForgetPassWordViewController ()

//手机号码
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTextfiled;
//验证码
@property (weak, nonatomic) IBOutlet UITextField *verifyTextField;
//密码
@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;
@property (weak, nonatomic) IBOutlet UITextField *surePasswdTextField;
//提交按钮
@property (weak, nonatomic) IBOutlet UIButton *tijiaoBtn;
//获取验证码按钮
@property (weak, nonatomic) IBOutlet UIButton *getVerifyBtn;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewHeightContraint;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property(nonatomic,strong)YJGCDTimer *timer;
@end

@implementation XFForgetPassWordViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LCOLOROFBACK;
}

-(void)initViews{
    [super initViews];
    _timer = [[YJGCDTimer alloc]init];
    self.navigationItem.title = @"密码重置";
    self.navigationController.navigationBar.translucent = NO;
    self.phoneNumTextfiled.smk_maxLength = 11;
}

-(void)setViewBoard:(UIView*)view{
    view.borderWhich = ZJViewBorderTop;
    view.borderWhich = ZJViewBorderBottom;
}

//提交按钮
- (IBAction)commitBtnClick:(id)sender {
    
    if (![self.passWordTextField.text isEqualToString:self.surePasswdTextField.text]) {
        [SOAutoHideMessageView showMessage:@"两次输入的密码不匹配，请检查" inView:self.view];
        return;
    }
    if (self.phoneNumTextfiled.text.length != 11) {
        [SOAutoHideMessageView showMessage:@"请输入正确的手机号码" inView:self.view];
    }else if (self.verifyTextField.text.length == 0){
        [SOAutoHideMessageView showMessage:@"请输入验证码" inView:self.view];
    }else if (self.passWordTextField.text.length == 0){
        [SOAutoHideMessageView showMessage:@"请输入密码" inView:self.view];
    }else if (self.surePasswdTextField.text.length == 0){
        [SOAutoHideMessageView showMessage:@"请输入确认密码" inView:self.view];
    }else{
        [self resightKeyBoard];
        NSString *password = self.passWordTextField.text.md5TolowercaseString;
        [self changePasswdRequestPhone:self.phoneNumTextfiled.text yzm:self.verifyTextField.text password:password];
    }
}

-(void)resightKeyBoard{
    [self.phoneNumTextfiled resignFirstResponder];
    [self.verifyTextField resignFirstResponder];
    [self.passWordTextField resignFirstResponder];
    [self.surePasswdTextField resignFirstResponder];
}


-(void)changePasswdRequestPhone:(NSString *)phone yzm:(NSString *)yzm password:(NSString *)password{
    WEAKSELF
    [XFPostRequest postChangePassWordByUserName:phone sms:yzm password:password success:^(id responseObject) {
        LGFLog(@" === %@",responseObject);
        if (responseObject == nil) return ;
        [SOAutoHideMessageView showMessage:responseObject[@"msg"] inView:self.view];
        if ([responseObject[@"status"] intValue] == 200) {

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [_timer cancel];
                self.getVerifyBtn.enabled = YES;
                [self.getVerifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                if (self.isFromLoginVC) {
                     [weakSelf.navigationController popViewControllerAnimated:YES];
                }else{
                    BaseNavigationController * Nav = [[BaseNavigationController alloc] initWithRootViewController:[LoginViewController new]];
                    kAppWindow.rootViewController = Nav;
                }
            });
        }
    } failure:^(NSError *error) {
        [SOAutoHideMessageView showMessage:LNETWORKERROR inView:self.view];
    }];
}

-(void)dealloc{
    [_timer cancel];
}

//获取验证码
- (IBAction)getVerifyBtnClick:(id)sender {
    [self.phoneNumTextfiled resignFirstResponder];
    if (self.phoneNumTextfiled.text.length != 11) {
        [SOAutoHideMessageView showMessage:@"请输入正确的手机号码" inView:self.view];
        return;
    }
    [self.phoneNumTextfiled resignFirstResponder];
    [self.verifyTextField resignFirstResponder];
    [self postGetVerifyBtn];
}

//获取验证码
-(void)postGetVerifyBtn{
    WEAKSELF
    [self showLoadingWithClear:YES];
    [XFPostRequest postChangePassWordGetVerifyCodeByUsername:self.phoneNumTextfiled.text success:^(id responseObject) {
        [weakSelf hideLoading];
        LGFLog(@"responseObject = %@",responseObject);
        [SOAutoHideMessageView showMessage:responseObject[@"msg"] inView:weakSelf.view];
        if ([responseObject[@"status"] intValue] == 200) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf startTimer];//倒计时
            });
        }else{
            
        }
    } failure:^(NSError *error) {
        LGFLog(@"%@",error);
        [weakSelf hideLoading];
        [SOAutoHideMessageView showMessage:LNETWORKERROR inView:self.view];
    }];
}

- (void)startTimer{
    self.getVerifyBtn.enabled = NO;
    WEAKSELF
    __block int count = 61;
    [_timer startWithTimeInterval:1.0 Handler:^{
        NSString *title = [NSString stringWithFormat:@"%d秒",--count];
        [weakSelf.getVerifyBtn setTitle:title forState:UIControlStateDisabled];
        if (count == 0){
            [weakSelf.getVerifyBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
            weakSelf.getVerifyBtn.enabled = YES;
            [weakSelf.timer cancel];
        }
    }];
}

@end
